<html>
<head><meta charset="utf-8"><title>HirId from DefId when the DefId is not local? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html">HirId from DefId when the DefId is not local?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225969496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225969496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225969496">(Feb 11 2021 at 10:04)</a>:</h4>
<p>Context: <a href="https://github.com/rust-lang/rust/issues/81991">#81991</a></p>
<p>Is there a way to get HirId of a DefId when the DefId is not local? When it's local I can do</p>
<div class="codehilite"><pre><span></span><code>tcx.hir().local_def_id_to_hir_id(id.as_local())
</code></pre></div>
<p>but I can't find anything like this when the id is not local. I'm guessing this is not possible in general, as I may not have HIR accessible for imported crates, but I wonder if it's possible in some cases, maybe with a function that returns <code>Option&lt;HirId&gt;</code>. More specifically I'm interested in types, and I'd expect this to be possible for types as otherwise if I'm checking an expression with an imported opaque expected type, I need to be able to compare that expected type with the type of the expression somehow.</p>



<a name="225969697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225969697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225969697">(Feb 11 2021 at 10:06)</a>:</h4>
<p>the hir only exists for the local crate</p>



<a name="225969722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225969722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225969722">(Feb 11 2021 at 10:06)</a>:</h4>
<p>so for non local <code>DefId</code>s there never exists a corresponding <code>HirId</code></p>



<a name="225969930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225969930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225969930">(Feb 11 2021 at 10:08)</a>:</h4>
<p>in your problem, you have a <code>ty::Opaque(extern_def_id, substs)</code> and a local expression?</p>



<a name="225970073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970073">(Feb 11 2021 at 10:10)</a>:</h4>
<p>and you want to know whether the local expression has the same type as the underlying type of the opaque type?</p>



<a name="225970277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970277">(Feb 11 2021 at 10:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F/near/225969930">said</a>:</p>
<blockquote>
<p>in your problem, you have a <code>ty::Opaque(extern_def_id, substs)</code> and a local expression?</p>
</blockquote>
<p>I have two <code>Opaque</code> types. Type of the local expression is also <code>Opaque</code>.</p>



<a name="225970298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970298">(Feb 11 2021 at 10:12)</a>:</h4>
<blockquote>
<p>and you want to know whether the local expression has the same type as the underlying type of the opaque type?</p>
</blockquote>
<p>Yeah</p>



<a name="225970506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970506">(Feb 11 2021 at 10:14)</a>:</h4>
<p>Hmm, I think I can just compare the <code>DefId</code>s, right?</p>



<a name="225970599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970599">(Feb 11 2021 at 10:15)</a>:</h4>
<p>that checks that u use the same <code>Opaque</code> type</p>



<a name="225970610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970610">(Feb 11 2021 at 10:16)</a>:</h4>
<p>you can use <code>type_of(extern_def_id)</code> to get the underlying type</p>



<a name="225970664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970664">(Feb 11 2021 at 10:16)</a>:</h4>
<p>and then <code>ty.subst(substs)</code> to get the thing you care about</p>



<a name="225970715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225970715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225970715">(Feb 11 2021 at 10:17)</a>:</h4>
<p>depending on what you want to do with the opaque types you can also normalize them with <code>Reveal::All</code> which does this for you</p>



<a name="225971182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/HirId%20from%20DefId%20when%20the%20DefId%20is%20not%20local%3F/near/225971182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/HirId.20from.20DefId.20when.20the.20DefId.20is.20not.20local.3F.html#225971182">(Feb 11 2021 at 10:22)</a>:</h4>
<p>Perfect, thanks <span class="user-mention" data-user-id="216206">@lcnr</span> . I'll try <code>type_of(extern_def_id)</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>